package cn.exrick.xboot.modules.content.controller;


import cn.exrick.xboot.common.utils.BeanConvertUtils;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.assessment.entity.HkFormEntity;
import cn.exrick.xboot.modules.assessment.service.HkFormEntityService;
import cn.exrick.xboot.modules.base.entity.Role;
import cn.exrick.xboot.modules.content.entity.*;
import cn.exrick.xboot.modules.content.entity.HontVo.HontDepartmentPostVo;
import cn.exrick.xboot.modules.content.entity.HontVo.HontPostVo;
import cn.exrick.xboot.modules.content.service.HontPostService;
import cn.exrick.xboot.modules.content.service.HontUserService;
import cn.exrick.xboot.modules.content.service.mybatis.IHontDepartmentPostService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(description = "岗位管理接口")
@RequestMapping("/xboot/hontPost")
public class HontPostController {
    @Autowired
    private HontPostService hontPostService;
    @Autowired
    private HontUserService hontUserService;
    @Autowired
    private HkFormEntityService hkFormEntityService;
    @Autowired
    private IHontDepartmentPostService ihontDepartmentPostService;


    @RequestMapping(value = "/getAllList",method = RequestMethod.GET)
    @ApiOperation(value = "获取全部岗位")
    public Result<Object> roleGetAll(){
        List<HontPost> list = hontPostService.getAll();
        return ResultUtil.data(list);
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存数据")
    public Result<HontPost> save(@RequestBody HontPostVo post){
        HontPost byTitle = hontPostService.findByTitle(post.getTitle());
        if (byTitle != null){
            ResultUtil.error("该岗位已存在");
        }
        HontPost hontPost = hontPostService.addHontPost(post);
        return new ResultUtil<HontPost>().setData(hontPost);
    }
    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<HontPost>> getByCondition(HontPost hontPost,
                                                SearchVo searchVo,
                                                PageVo pageVo,int current,int size){
        pageVo.setPageNumber(current);
        pageVo.setPageSize(size);
        Page<HontPost> page = hontPostService.findByCondition(hontPost, searchVo, PageUtil.initPage(pageVo));
        if(page.getContent().size()==0){
            return new ResultUtil<Page<HontPost>>().setData(page);
        }
        List<String> ids=page.getContent().stream().map(HontPost::getId).collect(Collectors.toList());
        List<HontDepartmentPostVo> maps=ihontDepartmentPostService.getHontPostByPosts(ids);
        for(HontPost hp: page.getContent()) {
            if (StrUtil.isNotBlank(hp.getFormId())){
                HkFormEntity hkFormEntity = hkFormEntityService.get(hp.getFormId());
                if (hkFormEntity != null){
                    hp.setFormTitle(hkFormEntity.getFormName());
                }
                List<HontDepartmentPostVo> departs=maps.stream().filter(t->hp.getId().equals(t.getId())).collect(Collectors.toList());
                if(departs.size()>0){
                    hp.setDepart(departs.get(0).getDepartments());

                }
            }
        }

        return new ResultUtil<Page<HontPost>>().setData(page);
    }
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Object> getByCondition(String postId){
        HontPost byPostId = hontPostService.get(postId);
        List<HontDepartmentPost> hontDepartmentPosts= ihontDepartmentPostService.getHontPostByPostId(postId);
        HontPostVo hontPostVo= BeanConvertUtils.convertBean(byPostId,HontPostVo.class);
        List<String> ids=hontDepartmentPosts.stream().map(HontDepartmentPost::getHontDepartmentId).collect(Collectors.toList());
        hontPostVo.setHontDepartmentPostIds(ids);
        return ResultUtil.data(hontPostVo);
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value = "修改数据")
    public Result<HontPost> update(@RequestBody HontPostVo post){
        return ResultUtil.data(hontPostService.updateHontPost(post));
    }
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    @ApiOperation(value = "修改数据")
    public Result<HontPost> del(HontPost post){
        List<HontUser> byPostId = hontUserService.findByPostId(post.getId());
        if(byPostId!=null&&byPostId.size()>0){
            return ResultUtil.error("删除失败，包含正被用户使用关联的岗位");
        }
        hontPostService.delete(post.getId());
        return ResultUtil.success("删除成功");
    }
}
